package com.atguigu.gulimall.order.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.gulimall.order.feign.ProductFiginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 订单
 *
 * @author wangxiaoliang
 * @email wangxiaoliang001@dingtaik.com
 * @date 2020-04-22 09:28:48
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;



    /**
     * 分页查登录用户的订单详情
     */
    @PostMapping("/listWithItem")
    public R listWithItem(@RequestBody Map<String, Object> params){
        System.out.println("/listWithItem....");
        PageUtils page = orderService.queryPageWithItem(params);
        return R.ok().put("page", page);
    }


    /**
     * 根据订单Id查询订单状态
     * @param orderSn
     * @return
     */
    @GetMapping("/status/{orderSn}")
    public R getOrderStatus(@PathVariable("orderSn")String orderSn){
        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);
        return R.ok().setData(orderEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @Resource
    ProductFiginService productFiginService ;


    @RequestMapping("/product/skuinfo/list")
    public R test(@RequestParam Map<String, Object> params){

       return productFiginService.list(params);

    }



    @GetMapping("/test1/{id}")
    @SentinelResource(value = "testB",blockHandler = "customerBlockHandler2",fallback = "customerBlockHandler3")
    public R testB(@PathVariable int id) {
        int a = 10/id ;
        System.out.println(productFiginService.list(null));
        return R.ok().put("data","按照用户自定义返回");

    }

    public  R customerBlockHandler2(@PathVariable  int id ,BlockException ex) {
        return R.error(444," blockHandler 用户自定义失败返回");
    }

    public  R customerBlockHandler3(@PathVariable  int id ,Throwable t) {
        return R.error(444," fallback 用户自定义失败返回");
    }
}
